package snippet;
/*
 * The MIT License (MIT)
 * 
 * Copyright (c) 2013 Jean-Sebastien Lerat (Jean-Sebastien.Lerat@ulb.ac.be)
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

/**
 * 
 * @author Jean-Sebastien Lerat (Jean-Sebastien.Lerat@ulb.ac.be)
 * @version 1.00, 24/06/2013
 */

import java.util.Arrays;
import java.util.Map;

import be.ac.ulb.mlg.utils.Measure;
import be.ac.ulb.mlg.utils.Measurer;
import be.ac.ulb.mlg.utils.TaxonRenormalizer;
import be.ac.ulb.mlg.utils.measure.*;

public class TaxaRun{
	private static final boolean REVERSE = false;
	private static final boolean REMOVE_ROOT = true;
	
	public static void main(String[] args) throws InterruptedException{
		//Defining data
		//1. The whole taxa
		String[] lineages = {
				construct(new String[]{"classA","familyA","Mu1"},REVERSE,REMOVE_ROOT),
				construct(new String[]{"classA","familyA","Ma2"},REVERSE,REMOVE_ROOT),
				construct(new String[]{"classA","familyA"},REVERSE,REMOVE_ROOT),
				construct(new String[]{"classA","familyB","mi1"},REVERSE,REMOVE_ROOT),
				construct(new String[]{"classA","familyB","me2"},REVERSE,REMOVE_ROOT),
				construct(new String[]{"classA","familyB"},REVERSE,REMOVE_ROOT),
				construct(new String[]{"classA","familyA","Mu1"},REVERSE,REMOVE_ROOT),
				construct(new String[]{"classA","familyA","Ma2"},REVERSE,REMOVE_ROOT),
				construct(new String[]{"classA","familyA"},REVERSE,REMOVE_ROOT),
				construct(new String[]{"classA","familyB","mi1"},REVERSE,REMOVE_ROOT),
				construct(new String[]{"classA","familyB","me2"},REVERSE,REMOVE_ROOT),
				construct(new String[]{"classA","familyB"},REVERSE,REMOVE_ROOT),
		};
		//2. The taxa
		double[][] data = {
			{0.1728684,		0.04798077,	0.1067830,	0.1279348,	0.2444661,	0.1273308,	0.2215162},//familyA-Mu1&&&oral
			{0.1311757,		0.2734506,	0.3863534,	0.321857,	0.1623215,	0.4661382,	0.3345042},//familyA-Ma2&&&oral
			{0.3,			0.31,		0.48,		0.44,		0.4,		0.58,		0.55},//classA-familyA&&&oral
			{0.4330092,		0.5962562,	0.4307061,	0.4134969,	0.5135985,	0.3345146,	0.3500739},//familyB-mi1&&&oral
			{0.2629466,		0.0823124,	0.07615744,	0.1367113,	0.07961386,	0.0720164,	0.09390566},//familyB-me2&&&oral
			{0.6959,		0.67,		0.56,		0.54,		0.58,		0.4,		0.44},//classA-familyB&&&oral
			{0.7762188,		0.7985296,	0.867136,	0.8128888,	0.8641904,	0.85988,	0.849397},//familyA-Mu1&&&stool
			{0.05565073,	0.03515018,	0.03398691,	0.08847002,	0.05130468,	0.04003465,	0.06780226},//familyA-Ma2&&&stool
			{0.82,			0.82,		0.89,		0.9,		0.94,		0.89,		0.92},//classA-familyA&&&stool
			{0.08830484,	0.1045147,	0.1118834,	0.1008723,	0.0928294,	0.08232475,	0.03495287},//familyB-mi1&&&stool
			{0.003887279,	0.02775721,	0.05823827,	0.03771083,	0.055024,	0.009979706,0.0179569},//familyB-me2&&&stool
			{0.091,			0.13,		0.16,		0.13,		0.14,		0.09,		0.05},//classA-familyB&&&stool
		};
		//3. The low taxa for each row
		String[] taxon = {
				"Mu1",
				"Ma2",
				"familyA",
				"mi1",
				"me2",
				"familyB",
				"Mu1",
				"Ma2",
				"familyA",
				"mi1",
				"me2",
				"familyB"
			};
		//4. Construct and setup the Taxa Renormalizer
		TaxonRenormalizer renorm = new TaxonRenormalizer();
		for(String taxa:lineages)
			renorm.addTaxa(taxa);
		renorm.setTaxa(taxon);

		//5. Print data
		System.out.println("DATA");
		println(data);
		
		//6. Construct and setup the measurer (1 thread, no missing values)
		Measurer measurer = new Measurer(false,0,renorm);
		measurer.setNumberOfThreads(1);
		measurer.setHandelingMissingValues(false);
		
		//7. compute results
		Map<Measure,double[][]> res = measurer.measure(
										data,
										new Measure[]{new Pearson()},
										null
									);
		//8. print resulting measures
		for(Measure m:res.keySet()){
			double[][] result = res.get(m);
			System.out.println(m.getClass().getSimpleName());
			println(result);
		}
		
		/*
		//9. Show the new mapping (into results) for the higher taxa
		int index=0;
		for(String higher:renorm.getNamedResult())
			System.out.println((index++)+": "+higher);
		*/
	}
	private static String construct(String[] lineage,boolean reverse,boolean rmRoot){
		StringBuffer buff = new StringBuffer();
		if(reverse){
			lineage = Arrays.copyOfRange(lineage, 0, lineage.length-1);
			for(String taxa:lineage){
				buff.insert(0, taxa);
				buff.insert(0,TaxonRenormalizer.DEFAULT_SEPARATOR);
			}
		}else{
			lineage = Arrays.copyOfRange(lineage, 1, lineage.length);
			for(String taxa:lineage){
				buff.append(TaxonRenormalizer.DEFAULT_SEPARATOR);
				buff.append(taxa);
			}
		}
		buff.delete(0,TaxonRenormalizer.DEFAULT_SEPARATOR.length());
		return buff.toString();
	}
	public static void println(double[][] data) {
		if(data == null){
			System.out.println("NULL");
			return;
		}
		for(double[] sub:data){
			System.out.print("\t");
			for(double val:sub)
				//System.out.printf("%6.2f",new Float(val));
				System.out.printf("   " +val);
			System.out.println();
		}
		
	}
} 
